<template>
    <div>
        <div class="operation">
            <a class="btn btn-theme" @click="showAddModal">添加单位</a>
            <a class="btn btn-theme" @click="showModifyModal">修改单位</a>
            <a class="btn btn-theme" @click="showDeleteModal">删除单位</a>
        </div>
        <table class="table">

        </table>
        <div class="modal fade" id="unit_add_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">添加部门</h4>
                                </div>
                                <div class="modal-body">
                                    <form>
                                        <div class="form-group">
                                            <label for="unit_add_name" class="control-label">单位名称：</label>
                                            <input type="text" class="form-control" id="unit_add_name">
                                        </div>

                                        <div class="form-group">
                                            <label for="unit_add_notes" class="control-label">单位备注：</label>
                                            <input type="text" class="form-control" id="unit_add_notes">
                                        </div>


                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="button" class="btn btn-primary" id="unit_add_submit" @click="addUnit">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="modal fade" id="unit_modify_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="exampleModalLabel">修改单位</h4>
                                </div>
                                <div class="modal-body">
                                    <form>
                                        <div class="form-group">
                                            <label for="unit_modify_name" class="control-label">单位名称：</label>
                                            <input type="text" class="form-control" id="unit_modify_name">
                                        </div>
                                        <div class="form-group">
                                            <label for="unit_modify_notes" class="control-label">备注：</label>
                                            <input type="text" class="form-control" id="unit_modify_notes">
                                        </div>

                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                    <button type="button" class="btn btn-primary" id="unit_modify_submit" @click="modifyUnit">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>


                    <div class="modal fade" id="unit_drop_modal">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title">删除确定</h4>
                                </div>
                                <div class="modal-body">
                                    <p>您真的要删除此单位吗？</p>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                    <button type="button" class="btn btn-primary" id="unit_drop_submit" @click="deleteUnit">确认</button>
                                </div>
                            </div>
                        </div>
                    </div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                tabledata: '',
                currentId: ''
            }
        },
        methods: {
            getTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/unit/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: false,
                    error(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success(res) {
                        _this.tabledata = res.Table2
                    }
                });
                $('.table').bootstrapTable('destroy')
                $('.table').bootstrapTable({
                    columns: [{
                        radio: true
                    }, {
                        field: 'fidno',
                        title: '单位id号',
                        halign: 'center',
                    }, {
                        field: 'fcname',
                        title: '单位名称',
                        halign: 'center',
                    }],
                    data: this.tabledata,
                    striped: true,
                    pagination: true,
                    selectItemName: 'unit',
                    sidePagination: 'client',
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 20, 50],
                    clickToSelect: true
                })
            },
            showAddModal() {
                $('#unit_add_modal').modal('show');
            },
            showModifyModal() {
                this.currentId = $(".selected").children().eq(1).text()
                if (this.currentId == '') {
                    this.$store.commit('showModal','请先选择要修改的单位')
                    
                } else {
                    $('#unit_modify_modal').modal('show');
                }
            },
            showDeleteModal() {
                this.currentId = $(".selected").children().eq(1).text()
                if (this.currentId == '') {
                    this.$store.commit('showModal','请先选择要删除的单位')
                    
                } else {
                    $('#unit_drop_modal').modal('show');
                }
            },
            addUnit() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/unit/add',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fcname': $('#unit_add_name').val(),
                        'fnotes': $('#unit_add_notes').val(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function() {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#unit_add_modal').modal('hide');
                        _this.getTable()
                    }
                });
            },
            modifyUnit() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/unit/update',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentId,
                        'fcname': $('#unit_modify_name').val(),
                        'fnotes': $('#unit_modify_notes').val(),
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function() {
                        _this.$store.commit('showModal','连接错误')
                        
                    },
                    success: function() {
                        $('#unit_modify_modal').modal('hide');
                        _this.getTable()
                    }
                });
            },
            deleteUnit() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/unit/delete',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': _this.currentId
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function() {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function() {
                        $('#unit_drop_modal').modal('hide');
                        _this.getTable()
                    }
                });
            }
        },
        mounted() {
            this.getTable()
        }
    }
</script>
<style>
    .operation {
        float: left;
        margin: 10px;
    }
</style>